package com.hjj.springboot.controller.rider;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjj.springboot.entity.*;
import com.hjj.springboot.service.IOrderDetailService;
import com.hjj.springboot.service.IOrderService;
import com.hjj.springboot.service.IOrderTipsService;
import com.hjj.springboot.service.IWxUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/rider/order")
public class RiderOrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderDetailService orderDetailService;

    @Autowired
    private IOrderTipsService orderTipsService;

    @Autowired
    private IWxUserInfoService wxUserInfoService;

    public static final List<Integer> RIDER_ORDER_status = Arrays.asList(5, 6, 7, 8); //骑手查看的已完成订单状态


    @RequestMapping("/getOrders")
    public R getOrders(Integer status , Integer id){
        List<Order> orderList;
        if(status == -1){
            orderList = orderService.list(new QueryWrapper<Order>().in("status", RIDER_ORDER_status).eq("riderId",id));
        }
        else {
            orderList = orderService.list(new QueryWrapper<Order>().eq("status", status).eq(status!=3,"riderId",id));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderList",orderList);
        return R.ok(map);
    }

    @RequestMapping("/getOrderDetail")
    public R getOrderDetail(Integer orderId){
        Order order = orderService.getById(orderId);
        OrderDetail[] goods = orderDetailService.list(new QueryWrapper<OrderDetail>().eq("mId",orderId)).toArray(new OrderDetail[0]);
        order.setGoods(goods);
        HashMap<String, Object> map = new HashMap<>();
        map.put("order",order);
        return R.ok(map);
    }

    @RequestMapping("/updateOrderStatus")
    public R updateOrderStatus(@RequestParam Integer orderId, @RequestParam Integer status,@RequestParam(required = false) Integer riderid){
        Order order = orderService.getById(orderId);
        order.setStatus(status);
        if (riderid!= null) {
            order.setRiderId(riderid);
        }
        orderService.updateById(order);
        return R.ok();
    }

    @GetMapping("/getOrderTips")
    public R getOrderTips(Integer riderid){
        List<Order> orderList = orderService.list(new QueryWrapper<Order>().eq("riderId",riderid));
        List<String> orderNoList = orderList.stream().map(Order::getOrderNo).collect(Collectors.toList());
        List<OrderTips> orderTipsList = orderTipsService.list(new QueryWrapper<OrderTips>().eq("type",4).in("orderNo",orderNoList));
        for(OrderTips orderTips : orderTipsList){
            orderTips.setOrder(orderService.getOne(new QueryWrapper<Order>().eq("orderNo",orderTips.getOrderNo())));
            orderTips.setWxUserInfo(wxUserInfoService.getOne(new QueryWrapper<WxUserInfo>().eq("openid",orderTips.getUId())));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderTipsList",orderTipsList);
        return R.ok(map);
    }

    @RequestMapping("/getNum")
    public R getNum(Integer id){
        int acceptOrder=orderService.count(new QueryWrapper<Order>().eq("status", 3));
        int deliveryOrder=orderService.count(new QueryWrapper<Order>().eq("status", 4).eq("riderId",id));
        int finishOrder=orderService.count(new QueryWrapper<Order>().eq("riderId",id).in("status", RIDER_ORDER_status));
        HashMap<String,Object> map=new HashMap<>();
        map.put("acceptOrder",acceptOrder);
        map.put("deliveryOrder",deliveryOrder);
        map.put("finishOrder",finishOrder);
        return R.ok(map);
    }
}
